import Vue from 'vue'
import { createApp } from './app'
import NProgress from 'nprogress'
const { app, router, store } = createApp()
import { IsPC } from 'util/util'
import infiniteScroll from 'vue-infinite-scroll'
Vue.use(infiniteScroll)
import VueLazyLoad from 'vue-lazyload'
//懒加载的默认图片 
import def_lazy_img from './assets/images/lazy_load.png'
//使用懒加载组件
Vue.use(VueLazyLoad,{
	loading: def_lazy_img
})
require('jquery')

if(IsPC()){
	require('assets/layer/theme/default/layer.css')
	require('assets/layer/layer')
}else{
	require('assets/layer/mobile/need/layer.css')
	require('assets/layer/mobile/layer')
}
if (window.__INITIAL_STATE__) {
  store.replaceState(window.__INITIAL_STATE__)
}
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })

router.onReady(() => {
  // Add router hook for handling asyncData.
  // Doing it after initial route is resolved so that we don't double-fetch
  // the data that we already have. Using router.beforeResolve() so that all
  // async components are resolved.
  router.beforeResolve((to, from, next) => {
    const matched = router.getMatchedComponents(to)
    const prevMatched = router.getMatchedComponents(from)
    let diffed = false
    const activated = matched.filter((c, i) => {
      return diffed || (diffed = (prevMatched[i] !== c))
    })
    if (!activated.length) {
      return next()
    }
    // start loading indicator
    Promise.all(activated.map(c => {
      NProgress.start()
      if (c.asyncData) {
        return c.asyncData({ store, route: to })
      }
    })).then(() => {
      // stop loading indicator
      NProgress.done()
      next()
    }).catch(next)
  })
  app.$mount('#app')
})

// service worker
if (window.location.protocol === 'https:' && navigator.serviceWorker) {
  navigator.serviceWorker.register('/service-worker.js')
}
